package com.zzx.boot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.emus.IsBuddy;
import com.zzx.boot.emus.UserOrigin;
import com.zzx.boot.entity.User;
import com.zzx.boot.entity.vo.UserInfoVo;
import com.zzx.boot.mapper.UserMapper;
import com.zzx.boot.service.BuddyService;
import com.zzx.boot.service.UserService;
import com.zzx.boot.utils.R;
import com.zzx.boot.utils.SecurityContext;
import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;

import java.rmi.registry.Registry;
import java.util.Objects;
import java.util.regex.Pattern;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService  {

    @Resource
    private UserMapper userMapper;
    @Resource
    private BuddyService buddyService;
    @Resource
    private SecurityContext securityContext;



    public static Pattern pattern = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");

    @Override
    public UserInfoVo getUserByUsernameAndPassword(String username, String password) {
        // 判断是否为手机号
        User user = null;
        if(pattern.matcher(username).matches()){
            user = userMapper.selectUserByUsernameAndPassword("",username, password);
        }else {
            user = userMapper.selectUserByUsernameAndPassword(username,"", password);
        }

        if(Objects.isNull(user)){
            throw new RuntimeException("密码错误或用户不存在");
        }
        // 获取用户信息
        UserInfoVo userInfoVo = this.getUserInfoByUserId(user.getId());
        securityContext.setUserInfo(userInfoVo);
        return userInfoVo;
    }

    @Override
    public UserInfoVo getUserByUsername(String username) {
        // 判断是否为手机号
        UserInfoVo user = null;
        UserOrigin type = null;
        if(pattern.matcher(username).matches()){
            user = userMapper.selectUserInfoByUsername("",username);
            type = UserOrigin.PHONE;
        }else {
            user = userMapper.selectUserInfoByUsername(username,"");
            type =UserOrigin.WEIXIN_NUMBER;
        }

        if(Objects.isNull(user)){
            throw new RuntimeException("用户信息不存在");
        }
        // 设置添加搜索渠道
        user.setSearchType(type.getCode());

        // 获取当前用户id
        Long userId = securityContext.getUserId();
        // 是否是自己
        if(userId.equals(user.getUserId())){
            user.setIsBuddy(IsBuddy.IS_MY.getCode());
            return user;
        }
        // 判断是否是好友
            // 1. 好友 显示信息
            // 2. 不是好友 显示添加按钮 (默认)
        user.setIsBuddy(buddyService.isBuddyByUserId(userId,user.getUserId()
                ,IsBuddy.IS_BUDDY.getCode())?IsBuddy.IS_BUDDY.getCode():IsBuddy.NO_BUDDY.getCode());
        return user;
    }

    @Override
    public UserInfoVo getUserInfoByUserId(Long id) {
        UserInfoVo infoVo = userMapper.getUserInfoById(id);
        if(Objects.isNull(infoVo)){
            throw new RuntimeException("用户信息不存在");
        }
        return infoVo;
    }
}
